const database = require("../../database/index");
const dayjs = require("dayjs");
// 消息新增
exports.add = (req, res) => {
    const created = dayjs().format("YYYY-MM-DD HH:mm:ss");
    const sql = "insert into chat_history set ?";
    database.query(
        sql,
        {
            ...req.body,
            created,
        },
        (err) => {
            if (err) return res.response(err);
            res.response("添加成功", 0);
        }
    );
};
// 消息删除
exports.delete = (req, res) => {
    const { id } = req.body;
    const sql = "delete from chat_history where id=?";
    database.query(sql, id, (err, result) => {
        if (err) return res.response(err);
        res.response("删除成功！", 0);
    });
};
// 消息查询
exports.list = (req, res) => {
    const { sender_id, receiver_id } = req.body;
    let sql = "select * from chat_history";
    let params = [];
    if (sender_id && receiver_id) {
        console.log(sender_id, receiver_id);
        sql += " where (sender_id = ? and receiver_id = ?) or (sender_id = ? and receiver_id = ?)";
        params.push(sender_id, receiver_id, receiver_id, sender_id);
    };
    database.query(sql, params, function (err, result) {
        if (err) return res.response(err);
        return res.send({
            code: 0,
            message: "success",
            result,
        });
    });
};